<?php

namespace App\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\Department
 */
class Department extends Model
{
    use SoftDeletes;

    const PARENT_COMPANY      = 1; //"company";
    const PARENT_DEPT         = 2; //"department";
    const PARENT_PROJECT      = 3; //"project";
    const PARENT_PART_COMPANY = 4; //"part_company";

    protected $hidden  = ['pivot'];
    protected $guarded = ['id'];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * 返回部门下的用户
     * @return BelongsToMany
     */
    function users()
    {
        return $this->belongsToMany( User::class , 'department_users' )
            ->withPivot( 'creator_id' ); //获取中间表字段
    }

    /**
     * 返回部门所属的公司
     */
    function company()
    {
        return $this->belongsTo(Company::class);
    }

    /**
     * 返回部门获取项目
     */
    function project()
    {
        return $this->belongsTo(Project::class);
    }

    /**
     * 返回部门所属参建单位
     */
    function partCompany()
    {
        return $this->belongsTo(PartCompany::class);
    }

}
